<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	class LocalUser
	{
		public $isOnline = false;
		public $id = false;
		public $sid = false;
		public $data_array = false;
		public $ranks_array = array();
		public $displayed_rank = false;
		
		// Constructor
		function LocalUser($connection)
		{
			if (isset($_COOKIE['id']) == true && isset($_COOKIE['sid']) == true)
			{
				$this->id = $connection->clean_var(intval($_COOKIE['id']));
				$this->sid = $connection->clean_var($_COOKIE['sid']);
				
				// Validate sid and check if the user is logged in.
				$row = $connection->get_result_array("SELECT * FROM user WHERE id='$this->id' AND sid='$this->sid'");
				
				// empty() will return true if $row is either empty or is false because of a query or connection error
				if (! empty($row))
				{
					$this->isOnline = true;
					
					$this->ranks_array = array();
					
					$this->data_array = $row;
					
					$result = $connection->query("SELECT rankid, displayedrank FROM user_ranks WHERE userid='$this->id'");
					
					while ($obj = $result->fetch_object())
					{
						// Add info to the ranks array
						array_push($this->ranks_array, $obj->rankid);
						
						// If it's the user's displayed rank then record it
						if ($obj->displayedrank == MYSQL_TRUE)
							$this->displayed_rank = $obj->rankid;
					}
					
					$result->free();
				}
			}
		}
		
		function has_rank($rankname, $connection)
		{
			$row = $connection->get_result_array("SELECT id FROM ranks WHERE name='$rankname'");
			
			if (empty($row))
			{
				echo $connection->get_error();
				return false;
			}
			
			foreach ($this->ranks_array as $rankid)
			{
				if ($rankid == $row['id'])
					return true;
			}
			
			return false;
		}
		
		function update_last_active($connection)
		{
			if (! $this->id)
			{
				return false;
			}
			
			if ($connection->query_noresult("UPDATE user SET lastactive=NOW() WHERE id='$this->id' AND sid='$this->sid'") == 0)
			{
				return false;
			}
			
			return true;
		}
	}
?>
